$.fn.extend

  # Plugin for creatin menu select boxes
  menubox: (options) ->
    settings =
      list: ''
      field: ''

    settings = $.extend settings, options

    $dropdown = null

    # Activate menu box according provided settings
    __activate = () ->
      $dropdown.show()

      $dropdown.find('li').bind 'click.selectboxitem', (event) ->
        __select event, @

      ($ '#menu-' + settings.field + '-select').parent().one 'mouseleave', ->
        __deactivate() if $dropdown.is ':visible'

      false

    # deactivate selectbox
    __deactivate = () ->
      $dropdown.hide().find('li').unbind 'click.selectboxitem'

      false

    # chose element with callback function and deactivate
    __select = (event, item) ->
      if $.isFunction settings.onselect
        settings.onselect.call this, event, settings.list, settings.field, item
      __deactivate()

    return @each () ->
      ($ @).mouseenter (event) ->

        $dropdown = ($ @).siblings('.submenu-select')
          .css('width', ($ @).innerWidth())

        __activate()



  